package love.yuanmeng.guetcoalsystem.cotroller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import love.yuanmeng.guetcoalsystem.configuration.AlipayConfig;
import love.yuanmeng.guetcoalsystem.pojo.Order;
import love.yuanmeng.guetcoalsystem.pojo.Prop;
import love.yuanmeng.guetcoalsystem.service.OrderService;
import love.yuanmeng.guetcoalsystem.util.DataGridView;
import love.yuanmeng.guetcoalsystem.util.ResultObj;
import love.yuanmeng.guetcoalsystem.vo.OrderPropNameVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/10/15 16:54
 */
@Controller
@CrossOrigin
@RequestMapping("Order")
public class OrderController {
    @Autowired
    OrderService orderService;

    /**
     * 买方查询个人订单
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/loadAllOrder")
    public DataGridView loadAllOrder(@Param("enid") String enid) {
        try {
            List<OrderPropNameVo> orderPropNameVos = orderService.loadAllOrder(enid);
            return new DataGridView(new Long(orderPropNameVos.size()), orderPropNameVos);
        } catch (Exception e) {
            return new DataGridView(-1, null);
        }

    }

    /*
    当买方缴纳完定金，
    导致牌子售出prop.propstatus=2，
    导致下单成功，买方等待收货order. status=1，
     */
    /**
     * 买方缴纳定金
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/paymentDeposit")
    public ResultObj paymentDeposit(@Param("enid") String enid) {
        try {
            return ResultObj.OPERATE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.OPERATE_ERROR;
        }

    }

    /**
     * 买方撤销订单
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/cancelOrder")
    public ResultObj cancelOrder(@Param("enid") String enid) {
        try {
            return ResultObj.OPERATE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.OPERATE_ERROR;
        }
    }

    @GetMapping("/OrderChangeStatus")
    @ResponseBody
    public String orderchangestatus(@Param("order_id")String order_id,@Param("id")String id) {
        return orderService.updateOrder(order_id,id);
    }


    @GetMapping("OrderPay")
    @ResponseBody
    public String orderpay(@Param("oid")String oid,@Param("oname") String oname,@Param("price") String price) {
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl("http://localhost:8080/#/OrderPaySuccess");
        //alipayRequest.setNotifyUrl("http://工程公网访问地址/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp");

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = oid;
        //付款金额，必填
        String total_amount = price;
        //订单名称，必填
        String subject = oname;
        //商品描述，可空
        String body = null;

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
        //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
        //		+ "\"total_amount\":\""+ total_amount +"\","
        //		+ "\"subject\":\""+ subject +"\","
        //		+ "\"body\":\""+ body +"\","
        //		+ "\"timeout_express\":\"10m\","
        //		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        //请求
        String result=null;
        try {

            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
        //输出

    }


}
